//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: 'Hello World',
    userInfo: {},
    hasUserInfo: false,
    canIUse: wx.canIUse('button.open-type.getUserInfo'),
    userPhoneNumber: '',
    phoneNum:'12345678900'
  },
  getPhoneNumber: function (e) {
    // console.log(e.detail.errMsg)
    // console.log(e.detail.iv)
    // console.log(e.detail.encryptedData)
    // 判断是否成功，若成功，发送服务器解密
    if (e.detail.errMsg === "getPhoneNumber:ok") {
      var sessionid = wx.getStorageSync("sessionid")
      if (sessionid !== "undefined") {
        // console.log('seid:'+sessionid);
        var that = this;
        wx.request({
          url: app.globalData.serverBase + '/user/getuserphone/',
          data: {
            sessionid: sessionid,
            encrypteddata: e.detail.encryptedData,
            iv: e.detail.iv
          },
          success: function (res) {
            console.log(res.data["phoneNumber"]);            
            that.setData({
              userPhoneNumber: res.data["phoneNumber"]
            })
          }
        })
      }else{
        console.log('error')
      }
    }
  },
  //事件处理函数
  bindViewTap: function () {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    if (app.globalData.userInfo) {
      //console.log(app.globalData.userInfo)
      this.setData({
        userInfo: app.globalData.userInfo,
        hasUserInfo: true
      })
    } else if (this.data.canIUse) {
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {          
          app.globalData.userInfo = res.userInfo;
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        }
      })
    }    
    //this.userLogin();
    //console.log(data.userInfo);
  },
  getUserInfo: function (e) {
    app.globalData.userInfo = e.detail.userInfo
    this.setData({
      userInfo: e.detail.userInfo,
      hasUserInfo: true
    })
  },
  userLogin: function () {
    wx.login({
      success: function (res) {
        if (res) {
          var code = res.code;
          // wx.request({
          //   url: 'https://api.weixin.qq.com/sns/jscode2session',
          //   data: {
          //     grant_type: 'authorization_code',
          //     appid: 'wx8292320488d40a87',
          //     secret: '0d677e7b2633583a35fef9b74c57e65a',
          //     js_code: code },
          //   success: function (res) {
          //     console.log(res.data)
          //   }
          // })
          wx.request({
            url: app.globalData.serverBase + '/user/authuser/',
            data: { code: code },
            success: function (res) {
              console.log(res.data["sessionid"])
              // sessionid save to localStorge
              wx.setStorage({
                key: "sessionid",
                data: res.data["sessionid"]
              })
            }
          })
          console.log('获取用户登录凭证：' + res.code);
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    });
  },
  tabScanCode:function(){
    var that = this
    wx.scanCode({
      success: (res) => {
        //console.log(res)
        if (res.errMsg === "scanCode:ok"){
          console.log(res.result)
          that.setData({
            scancode: res.result
          })
        }
        
      }
    })
  },
  tabCallPhone:function(e){
    var phonenumber = e.currentTarget.id
    console.log(phonenumber)
    wx.makePhoneCall({
      phoneNumber: phonenumber //仅为示例，并非真实的电话号码
    })
  },
  tabGetLocation:function(){
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success: function (res) {
        var latitude = res.latitude
        var longitude = res.longitude
        wx.openLocation({
          latitude: latitude,
          longitude: longitude,
          scale: 28
        })
      }
    })
  },
  tabOpenLocation:function(){
    var latitude = this.data.currentlatitude
    var longitude = this.data.currentlongitude
    console.log('latitude:' + latitude + ';longitude:' + longitude);
    wx.openLocation({
      latitude: latitude,
      longitude: longitude,
      scale: 28
    })
  },
  tabChooessLocation:function(){
    var that = this;
    wx.chooseLocation({
      success: function(res) {
        if (res.errMsg === "chooseLocation:ok"){
          console.log(res)
          that.setData({
            currentlatitude: res.latitude,
            currentlongitude: res.longitude
          })
        }
      },
    })
  }
})

